/*
 * 创建日期 2008-3-3
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.struts.ContextLoaderPlugIn;

/**
 * rem: 页面工具 aut: 王文成 ver: 1.0 log: 2008-3-3
 */
public final class WebUtil {

    private WebUtil() {

    }

    /**
     * request & session 范围寻找属性值
     * 
     * @param request
     * @param key
     * @return
     */
    public static Object findAttribute(HttpServletRequest request, String key) {
        return findAttribute(request, key, null);
    }

    /**
     * request & session 范围寻找属性值
     * 
     * @param request
     * @param key
     * @param defualtValue
     * @return
     */
    public static Object findAttribute(HttpServletRequest request, String key, Object defualtValue) {
        HttpSession session = request.getSession();
        Object requestScopeValue = request.getAttribute(key);
        Object sessionScopeValue = session.getAttribute(key);
        Object value = tryThese(new Object[] { requestScopeValue, sessionScopeValue });
        return value != null ? value : defualtValue;
    }

    /**
     * request & session 范围寻找属性值
     * 
     * @param request
     * @param key
     * @param defualtValue
     * @return
     */
    public static ServletContext getServletContext(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return session.getServletContext();
    }

    /**
     * 取得WebApplicationContext
     * 
     * @param request
     * @return
     */
    public static WebApplicationContext getWebApplicationContext(HttpServletRequest request) {
        ServletContext sc = request.getSession().getServletContext();
        return getWebApplicationContext(sc);
    }

    /**
     * 取得SptingBean实例
     * 
     * @return
     */
    public static Object getBean(HttpServletRequest request, String id) {
        return getWebApplicationContext(request).getBean(id);
    }
    
    /**
     * 取得Bean
     * @param sc
     * @param id
     * @return
     */
    public static Object getBean(ServletContext sc,String id) {
        return getWebApplicationContext(sc).getBean(id);
    }

    /**
     * 取得WebApplicationContext
     * 
     * @param request
     * @return
     */
    public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
        //通过web.xml 配置 org.springframework.web.context.ContextLoaderListener 加载。
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        if (ctx == null) {
            // 从struts-config.xml插件方式加载的
            ctx = (WebApplicationContext) sc.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
            if (ctx == null) {
                throw new RuntimeException(
                        "WebApplicationContext error ........  Can't find [WebApplicationContext] in any scope !");
            }
        }
        return ctx;
    }

    /**
     * 取得SptingBean实例
     * 
     * @return
     */
    public static Object getBean(HttpSession session, String id) {
        return getWebApplicationContext(session.getServletContext()).getBean(id);
    }

    /**
     * 取得有效值
     * 
     * @param objects
     * @return
     */
    private static Object tryThese(Object[] objects) {
        for (int i = 0; i < objects.length; i++) {
            if (StringUtil.isValid(objects[i]))
                return objects[i];
        }
        return null;
    }
}
